package com.magic233.service;

import com.magic233.entity.Depts;
import com.magic233.entity.Users;
import com.magic233.entity.WorkType;
import com.magic233.mapper.DeptsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ClassName: DeptsServiceImpl
 * Description:
 * date: 2021/6/3 8:32
 * author: zouyuan
 */

@Service
public class DeptsServiceImpl implements DeptsService {
    @Autowired
    private DeptsMapper deptsMapper;

    @Autowired
    private LogsService logsService;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public int addDept(Users user, String dept) {
        Long id = new Date().getTime();
        Depts depts = new Depts();
        depts.setDept(dept);
        depts.setCreatetime(simpleDateFormat.format(id));
        depts.setCreateid(user.getId());

        int re = deptsMapper.insert(depts);
        if (re == 1) {
            String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}新增了部门：" + dept;
            logsService.addLog(thing);
        }
        return re;
    }

    @Override
    public List<Depts> selectDept() {
        List<Depts> depts = deptsMapper.selectAll();
        return depts;
    }

    @Override
    public int deleteDept(Users user, String dept) {
        int re = deptsMapper.deleteByDept(dept);
        if (re == 1) {
            String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}删除了部门：" + dept;
            logsService.addLog(thing);
        }
        return re;
    }
}
